<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <h2>1. 事件处理方法</h2>
        <button @click="say">Say {{msg}}</button>
        <button @click="warn('hello', $event)">Warn</button>
        <div @click="todo">
            <button @click="todoThis">默认事件冒泡</button>
        </div>
        <div @click="todo">
            <button @click.stop="todoThis">阻止事件冒泡</button>
        </div>
        <br>
        <br>
        <br>

        <a href="https://www.baidu.com" target="_blank" @click.prevent="todoThis">百度一下</a>

        <button @click.once="doOnly">点击事件只会触发一次:{{num}}</button>


        <h2>3. 按键修饰符</h2>
        <input @keyup.enter="keyEnter">
        <!--进入输入框按回车时调用keyEnter-->
        <input @keyup.space="keySpace">
        <!--进入输入框按回车时调用keySpace-->

        <input type="text" autofocus>

    </div>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script type="text/javascript">
        var vm = new Vue({
            el: '#app',
            data: {
                msg: 'Hello, Vue.js',
                num: 0
            },
            methods: {
                say: function (event) {// `this` 在方法里指向当前 Vue 实例
                    alert(this.msg) // `event` 是原生 DOM 事件
                    alert(event.target.innerHTML)
                    console.log(event)
                },//多个参数如果需要使用原生事件，将 $event 作为实参传入
                warn: function (msg, event) {
                    alert(msg + "," + event.target.tagName)
                },
                todoThis: function () {
                    alert("todoThis .....")
                },
                todo: function () {
                    alert("todo....")
                },
                doOnly: function () {
                    this.num++
                },
                keyEnter: function () {
                    alert("已按：回车键")
                },
                keySpace: function () {
                    alert("已按：空格键")
                }
            }
        })</script>
</body>